package com.ouzexi.finalexp

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.Looper
import android.util.Log
import android.widget.*
import com.google.gson.Gson
import com.ouzexi.finalexp.adapter.PositionAdapter
import com.ouzexi.finalexp.util.save
import com.ouzexi.finalexp.vo.Favor
import com.ouzexi.finalexp.vo.Position
import okhttp3.*
import java.io.IOException

import java.math.BigDecimal

class admiring_position : AppCompatActivity() {

    lateinit var token:String

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_admiring_position)

        token = save().getData(this,"token")

        getFavorPosition()

        // 模拟数据
//        pos.add(
//            Position(1,
//            "后端程序员",
//            "校招",
//            BigDecimal("5000"),
//            "00",
//            "本科",
//            "无需工作经验",
//            "东莞",
//            "熟悉计算机网络，数据结构和操作系统")
//        )
    }

    fun initView(result: String?) {
        val favor = Gson().fromJson(result, Favor::class.java)

        if(favor.code!=2){
            // 到时可以传一个自增的id 然后跳转时根据id获取岗位详情信息
            val pos = ArrayList<Position>()

            runOnUiThread{
                for(i in favor.data.indices) {
                    pos.add(
                        Position(1,
                            favor.data[i].jname,
                            "favor.data[i].jtypename.toString()",
                            BigDecimal(favor.data[i].payLow*1000),
                            "00",
                            favor.data[i].demand,
                            favor.data[i].responsibility,
                            favor.data[i].ename,
                            favor.data[i].period)
                    )
                }
                // 获取ListView组件
                val List = findViewById<ListView>(R.id.List)

                val adapter = PositionAdapter(this,R.layout.position_item,pos)
                List?.adapter  = adapter

                // 点击监听器 转到岗位详情
                List.onItemClickListener = AdapterView.OnItemClickListener { parent, view, position, id ->
                    Log.i("testClick","ClickListen")
                    val intent = Intent(this, detail::class.java)
                    // intent.putExtra("id",id)
                    // Log.i("testid",id.toString())
                    intent.putExtra("jname",favor.data[id.toInt()].jname)
                    // 这个字段老马数据库系空的，所以会报错
                    intent.putExtra("jtype","这个字段老马数据库系空的，所以会报错")
                    intent.putExtra("jpay",favor.data[id.toInt()].payLow*1000)
                    intent.putExtra("jst","00")
                    intent.putExtra("jdemand",favor.data[id.toInt()].demand)
                    intent.putExtra("jresponsibility",favor.data[id.toInt()].responsibility)
                    intent.putExtra("jename",favor.data[id.toInt()].ename)
                    intent.putExtra("jperiod",favor.data[id.toInt()].period)
                    intent.putExtra("jid",favor.data[id.toInt()].id)
                    startActivity(intent)
                }
            }
        }
        else{
            Looper.prepare()
            Toast.makeText(this,"没有数据了",Toast.LENGTH_LONG).show()
            Looper.loop()
        }
    }

    private fun getFavorPosition() {
        val urlAPI = "http://10.0.2.2:8080/jobs/favor-position/getFavorPosition"
        Log.i("minetoken", token)
        val request = Request.Builder()
            .url(urlAPI)
            .addHeader("token", token)
            .get()
            .build()

        val client = OkHttpClient()
        client.newCall(request).enqueue(object : Callback {
            override fun onResponse(call: Call, response: Response) {
                val result = response.body?.string()
                Log.i("test",result.toString())
//                JSONObject.parseObject(result)
                initView(result)
            }
            override fun onFailure(call: Call, e: IOException) {
                println("Failed request api :( " + e.message)
            }
        })

    }
}